# This method was downloaded from http://jason-parks.com/Downloads/PySide/loadUiType.py and was modified to make it compatible
# with Python 3 as it was written for Python 2.

import pysideuic
import xml.etree.ElementTree as xml
from PySide import QtGui

# from cStringIO import StringIO    # for python 2 
import io   # for python 3

def loadUiType(uiFile):
        """
        Pyside lacks the "loadUiType" command, so we have to convert the ui file to py code in-memory first
        and then execute it in a special frame to retrieve the form_class.
        """
        parsed = xml.parse(uiFile)
        widget_class = parsed.find('widget').get('class')
        form_class = parsed.find('class').text
        
        with open(uiFile, 'r') as f:
            o = io.StringIO()   # o = StringIO() #    for python 2 
            frame = {}
            
            pysideuic.compileUi(f, o, indent=0)
            pyc = compile(o.getvalue(), '<string>', 'exec')
            # exec pyc in frame    # for python 2 
            exec(pyc, frame)       # for python 3
            
            #Fetch the base_class and form class based on their type in the xml from designer
            form_class = frame['Ui_%s'%form_class]
            base_class = eval('QtGui.%s'%widget_class)
#             base_class = eval('PySide.QtGui.%s'%widget_class)
        return form_class, base_class
